import {defineStore} from 'pinia'
import dayjs from "dayjs";
import {sendLogs} from "@/api/user";


const useLogStore = defineStore('logStore', {
    state: () => {
        return {
            logsList: []
        }
    },
    getters: {

        logsLen: state => state.logsList.length || 0,
        logsFlag: state => state.logsList.length || 0
    },
    actions: {
        //发送错误日志
        SendLogs() {
            return new Promise((resolve, reject) => {
                sendLogs(this.logsList).then(() => {
                    this.clearLogs()
                    resolve();
                }).catch(error => {
                    reject(error)
                })
            })
        },
        addLog({type, message, stack, info}) {
            let item = {}
            item.url = window.location.href
            item.time = dayjs().format('YYYY-MM-DD HH:mm:ss')
            item.type = type
            item.message = message
            item.stack = stack
            item.info = info.toString()
            this.logsList.push(item)
        },
        clearLogs() {
            this.logsList = [];
        }
    },
    persist: true
})

export default useLogStore
